/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sgarreau.android.gsb;

import Model.DAO;
import Model.Medecin;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author sgarreau
 */
public class MedecinActivity extends ListActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //on va chercher la vue
        setContentView(R.layout.medecinactivity);
        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String dep = inter.getStringExtra("leDep");
        final List<Medecin> lesMeds = DAO.getLesNoms(dep);
        label.setText(dep);
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);
        final MedecinActivity medAct = this;

        Button button = (Button) findViewById(R.id.buttonFiltre);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //En cas de click sur le bouton
                EditText edit = (EditText) findViewById(R.id.filter);
                String texte = edit.getText().toString();
                List<Medecin> lesMedsFiltrer = new ArrayList<Medecin>();
                for (Medecin unMed : lesMeds) {
                    if (unMed.getNom().startsWith(texte)) {
                        lesMedsFiltrer.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsFiltrer, medAct);
                setListAdapter(adapter);
            }

        });

    }

    protected void onListItemClick(ListView l, View v, final int position, final long id) {

        String uri = "tel:"+((Medecin)l.getItemAtPosition(position)).getTel();
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse(uri));
        startActivity(intent);
    }

}
